package com.lufei.dh.bottom.Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lufei.dh.R;
import com.lufei.dh.more.Bean.BeanMore;

import java.util.ArrayList;
import java.util.List;

/**
 * 版权：易金卡沃 版权所有
 * <p>
 * 作者：lufei
 * <p>
 * 创建日期：2018/6/29
 * <p>
 * 描述：
 * <p>
 * 修订历史：
 */
public class AdapterLF extends RecyclerView.Adapter<AdapterLF.ViewHodler> {
    private List<BeanMore> data;
    Context context;
    private LayoutInflater layoutInflater;


    public AdapterLF(List<BeanMore> data, Context context) {
        this.data = data;
        this.context = context;
        layoutInflater = LayoutInflater.from(context);

    }

    /**
     * 原生adapter多布局关键方法
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        return data.get(position).getItemType();
    }

    @Override
    public ViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {


        if (viewType == 1) {
            View view1 = layoutInflater.inflate(R.layout.item_category, parent, false);
            ViewHodler viewHodler = new ViewHodler(view1);

            return viewHodler;
        } else if (viewType == 2) {
            View view = layoutInflater.inflate(R.layout.item_two, parent, false);
            Log.e("zzzzz", "111111111");
            ViewHodler viewHodler = new ViewHodler(view);
            return viewHodler;
        } else if (viewType == 3) {
            View view = layoutInflater.inflate(R.layout.item_song, parent, false);

            ViewHodler viewHodler = new ViewHodler(view);
            return viewHodler;
        } else {
            View view = layoutInflater.inflate(R.layout.item_category, parent, false);
            ViewHodler viewHodler = new ViewHodler(view);
            return viewHodler;
        }

    }

    @Override
    public void onBindViewHolder(ViewHodler holder, final int position) {
        BeanMore beanMore = data.get(position);
        if (data.get(position).getItemType() == 1) {
            holder.title.setText(beanMore.getTitle());
            holder.time.setText(beanMore.getDate());
            Glide.with(context).load(beanMore.getThumbnail_pic_s()).into(holder.imageView1);

        } else if (data.get(position).getItemType() == 2) {
            holder.title.setText(beanMore.getTitle());
            holder.time.setText(beanMore.getDate());
            Glide.with(context).load(beanMore.getThumbnail_pic_s()).into(holder.imageView1);
            Glide.with(context).load(beanMore.getThumbnail_pic_s02()).into(holder.imageView2);

        } else if (data.get(position).getItemType() == 3) {
            holder.title.setText(beanMore.getTitle());
            holder.time.setText(beanMore.getDate());
            Glide.with(context).load(beanMore.getThumbnail_pic_s()).into(holder.imageView1);
            Glide.with(context).load(beanMore.getThumbnail_pic_s02()).into(holder.imageView2);
            Glide.with(context).load(beanMore.getThumbnail_pic_s03()).into(holder.imageView3);
        } else {
            holder.title.setText(beanMore.getTitle());
            holder.time.setText(beanMore.getDate());
            Glide.with(context).load(beanMore.getThumbnail_pic_s()).into(holder.imageView1);
        }
        setAction(holder.itemView, position);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public static class ViewHodler extends RecyclerView.ViewHolder {
        private TextView title;

        private TextView time;
        private ImageView imageView1;
        private ImageView imageView2;
        private ImageView imageView3;

        public ViewHodler(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            time = itemView.findViewById(R.id.time);
            imageView1 = itemView.findViewById(R.id.image1);
            imageView2 = itemView.findViewById(R.id.image2);
            imageView3 = itemView.findViewById(R.id.image3);
        }
    }

    public static interface OnPlayClickListener {
        // true add; false cancel
        public void onItemClick(int position);
    }

    // add click callback
    OnPlayClickListener onItemPlayClick;

    public void setOnPlayClickListener(OnPlayClickListener onItemPlayClick) {
        this.onItemPlayClick = onItemPlayClick;
    }

    private void setAction(final View view, final int position) {
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemPlayClick != null) {
                    onItemPlayClick.onItemClick(position);
                }
            }
        });
    }


}
